//
//  WBHomeViewController.swift
//  XLWB
//
//  Created by MacBook on 16/6/29.
//  Copyright © 2016年 MacBook. All rights reserved.
//

import UIKit

//原创cell
private let originaCellId = "originaCellId"

//转发cell
private let retweetedCellId = "retweetedCellId"
class WBHomeViewController: WBBaseViewController {

//    private lazy var statusList = [String]()
    private lazy var listViewModel = WBStatusListViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()

       
    }
    
    
    override func loadData() {

        print("准备刷新")
        refreshControl?.beginRefreshing()
        
        //模拟演示加载数据
        DispatchQueue.main.after(when: DispatchTime.now() + 2) {
            
            self.listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess, shouldRefresh) in
                
                print("加载数据结束")
                //结束刷新
                self.refreshControl?.endRefreshing()
                
                //恢复上拉刷新标记
                self.isPullup = false
                //刷新
                if shouldRefresh {
                    
                    self.tableView?.reloadData()
            }
        
            }
        
        }
    }

   @objc private func showFriends() {
    
        let vc = WBDemoViewController()
//        vc.hidesBottomBarWhenPushed = true
        
        navigationController?.pushViewController(vc, animated: true)
    
    }

}

extension WBHomeViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return listViewModel.statusList.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        //取出视图模型,根据视图模型判断可重用cell
        let vm = listViewModel.statusList[indexPath.row]
        
        let cellId = (vm.status.retweeted_status != nil) ? retweetedCellId : originaCellId
        
        //FIXME====修改CellId
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! WBStatusCell
        
//        let viewMod = listViewModel.statusList[indexPath.row]
//        cell.viewModel = viewMod
        
       cell.viewModel = vm
        cell.delegate = self
        return cell
    }
    
    //父类i必须实现代理方法,子类才能够重写 2.0不需要
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        //根据indexPath获取视图模型
        let vm = listViewModel.statusList[indexPath.row]
        
        //返回计算好的行高
        return vm.rowHeight
    }
    
}
// MARK: - WBStatusCellDelegate
extension WBHomeViewController: WBStatusCellDelegate {
    
    func statusCellDidSelectedURLString(cell: WBStatusCell, urlString: String) {
        
        print(urlString)
        let vc = WBWebViewController()
        
        vc.urlString = urlString
        
        navigationController?.pushViewController(vc, animated: true)

    }
}



//MARK--设置界面
extension WBHomeViewController {

    override func setupTableView() {
        super.setupTableView()
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友",target: self,action: #selector(showFriends))
        
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: originaCellId)
        
        tableView?.register(UINib(nibName: "WBStatusRetweetedCell", bundle: nil), forCellReuseIdentifier: retweetedCellId)
    
//        tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight = 350
        
        //取消分割线
        tableView?.separatorStyle = .none
        setupNavTitle()
    }
    private func setupNavTitle() {
    
        let title = WBNetworkManager.shared.userAccount.screen_name

        let button = WBTitleButton(title: title)
        navItem.titleView = button
        
        button.addTarget(self, action: #selector(clickTitleBtn), for: .touchUpInside)
    
    }
    @objc func clickTitleBtn(btn: UIButton) {
    
    btn.isSelected = !btn.isSelected
    
    }
}
